package com.crm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.crm.domain.Business;
import com.crm.domain.Clue;
import com.crm.domain.Contract;
import com.crm.dto.SalesStatisticDTO;
import com.crm.dto.VulnerabilityReportDTO;
import com.crm.mapper.BusinessMapper;
import com.crm.mapper.ClueMapper;
import com.crm.mapper.ContractMapper;
import com.crm.service.ReportService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.math.BigDecimal;
import java.util.List;
import java.util.Objects;

@Service
public class ReportServiceImpl implements ReportService {

    @Resource
    private ClueMapper clueMapper;

    @Resource
    private BusinessMapper businessMapper;

    @Resource
    private ContractMapper contractMapper;

    @Override
    public VulnerabilityReportDTO getVulnerabilityMap(LocalDate startDate, LocalDate endDate) {
        // 把 LocalDate 转换为 LocalDateTime 范围
        LocalDateTime startDateTime = startDate.atStartOfDay();
        LocalDateTime endDateTime = endDate.atTime(LocalTime.MAX);

        VulnerabilityReportDTO dto = new VulnerabilityReportDTO();
        // 线索总数
        QueryWrapper<Clue> clueWrapper = new QueryWrapper<>();
        clueWrapper.between("create_time", startDateTime, endDateTime);
        Long cluesCount = clueMapper.selectCount(clueWrapper);
        dto.setCluesNums(cluesCount);
        // 有效线索数
        QueryWrapper<Clue> effectiveClueWrapper = new QueryWrapper<>();
        effectiveClueWrapper.eq("status", 1).between("create_time", startDateTime, endDateTime);
        Long effectiveCluesCount = clueMapper.selectCount(effectiveClueWrapper);
        dto.setEffectiveCluesNums(effectiveCluesCount);
        // 商机总数
        QueryWrapper<Business> businessWrapper = new QueryWrapper<>();
        businessWrapper.between("create_time", startDateTime, endDateTime);
        Long businessCount = businessMapper.selectCount(businessWrapper);
        dto.setBusinessNums(businessCount);
        // 合同总数
        QueryWrapper<Contract> contractWrapper = new QueryWrapper<>();
        contractWrapper.between("create_time", startDateTime, endDateTime);
        Long contractCount = contractMapper.selectCount(contractWrapper);
        dto.setContractNums(contractCount);
        return dto;
    }

    @Override
    public List<SalesStatisticDTO> getSalesStatistic(String beginCreateTime, String endCreateTime) {
        return List.of();
    }
}
